package com.smec.remes.module.user;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.TextView;

import com.smec.moblie.R;
import com.smec.moblie.databinding.ActivityUserBinding;
import com.smec.remes.base.SmecBaseActivity;
import com.smec.remes.bean.ChangeStyle;
import com.smec.remes.module.request.SmecRequestPresenter;
import com.smec.remes.module.user.fragment.SmecCompanyFragment;
import com.smec.remes.module.user.fragment.SmecStationFragment;
import com.smec.remes.util.CommonUtils;
import com.zyyoona7.lib.EasyPopup;
import com.zyyoona7.lib.HorizontalGravity;
import com.zyyoona7.lib.VerticalGravity;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/5.
 */

public class SmecUserActivity  extends SmecBaseActivity<SmecRequestPresenter> {

    private ActivityUserBinding activityUserBinding;
    public static final String USERNAME="USERNAME";
    private ArrayList<Fragment> fragmentArrayList=new ArrayList<>();
    private ChangeStyle changeStyle=new ChangeStyle();
    private String prompt="当前客户所属维保站，所有有效保养合同的合同号梯号\n" +
            "在保养合同有效期内，以及保养合同到期后三个月内的电梯为有效保养合同";
    private EasyPopup mCirclePop;

    @Override
    public SmecRequestPresenter getPresenter() {
        return new SmecRequestPresenter(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityUserBinding=DataBindingUtil.setContentView(this,R.layout.activity_user);
        changeStyle.setOne(0);
        activityUserBinding.setChange(changeStyle);
        _edit();
        _HideKeyboard();
        _changeStyle();
        _init();
        _return();
        _prompt();
    }

    public void _return(){
        activityUserBinding.userTopbar.getTvTextLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SmecUserActivity.this.finish();
                mRxBus.post("UserEditNew","true");
            }
        });
    }

    public void _init(){
        fragmentArrayList.add(SmecStationFragment.newInstance());
        fragmentArrayList.add(SmecCompanyFragment.newInstance());
        activityUserBinding.viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentArrayList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentArrayList.size();
            }

        });
    }

    public void _changeStyle(){
        activityUserBinding.btnStation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeStyle.setOne(0);
                activityUserBinding.invalidateAll();
                activityUserBinding.viewPager.setCurrentItem(0);
            }
        });

        activityUserBinding.btnCompany.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeStyle.setOne(1);
                activityUserBinding.invalidateAll();
                activityUserBinding.viewPager.setCurrentItem(1);
            }
        });
    }

    public void _HideKeyboard(){
        activityUserBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                CommonUtils.hideImmManager(activityUserBinding.getRoot());
            }
        },100);
    }

    public void _edit(){
        activityUserBinding.userTopbar.gettvTextRightSecond().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SmecUserActivity.this.finish();
                mRxBus.post("UserEdit","Edit");
            }
        });
    }

    public void _prompt(){
        activityUserBinding.ivPrompt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(changeStyle.getOne()==0){
                    prompt="可查询范围为当前维保站下，电梯所属有效保养合同的对应客户名称。";
                }else {
                    prompt="可查询范围为当前分公司下，电梯所属有效保养合同的对应客户名称。";
                }
                _getPopuWindow(activityUserBinding.ivPrompt);
            }
        });
    }

    private void _getPopuWindow(View view){
        mCirclePop=new EasyPopup(this)
                .setContentView(R.layout.layout_prompt)
                .setAnimationStyle(R.style.QQPopAnim)
                //是否允许点击PopupWindow之外的地方消失
                .setFocusAndOutsideEnable(true)
                .setWidth(900)
                .setHeight(180)
                //指定任意 ViewGroup 背景变暗
                .createPopup();

        mCirclePop.showAtAnchorView(view, VerticalGravity.CENTER, HorizontalGravity.LEFT, 0, 0);

        TextView smecCompare=mCirclePop.getView(R.id.tv_prompt);
        smecCompare.setText(prompt);
    }
}
